// 将路由嵌套的数据拉平 深度优先遍历
import { getStream } from 'nc-uesdk';
import * as MathUtils from './MathUtils';
export const getModelByName = (name) => {
  const stream = getStream();
  const sessionId = MathUtils.generateUUID();
  return new Promise(function (resolve) {
    const str = { function: 'getModelByName', ModelName: `${name}`, sessionId: sessionId };
    stream.emitUIInteraction(str);
    const getGlbPosition = (data) => {
      // {"ModelName":"","UELocation":""}
      const returnData = JSON.parse(data);
      console.log('模型位置', data);
      if (returnData.sessionId === sessionId) {
        console.log('模型位置', returnData);
        stream.removeResponseEventListener(`dataSmithPosition${sessionId}`);
        resolve(returnData);
      }
    };
    stream.addResponseEventListener(`dataSmithPosition${sessionId}`, getGlbPosition);
  });
};
export const getDistanceType = (state) => {
  console.log('state', state);
  const stream = getStream();
  const sessionId = MathUtils.generateUUID();
  return new Promise(function (resolve) {
    const str = { function: 'getDistanceType', DistanceVisibility: `${state}`, sessionId: sessionId };
    stream.emitUIInteraction(str);
    const getGlbPosition = (data) => {
      // {"ModelName":"","UELocation":""}
      const returnData = JSON.parse(data);
      console.log('模型位置--', returnData);
      stream.removeResponseEventListener(`dataSmithPosition`);
      resolve(returnData);
      // if (returnData.sessionId === sessionId) {
      //   console.log('模型位置', returnData);
      //   stream.removeResponseEventListener(`dataSmithPosition${sessionId}`);
      //   resolve(returnData);
      // }
    };
    stream.addResponseEventListener(`dataSmithPosition`, getGlbPosition);
  });
};
export const getSaveDistance = (name) => {
  const stream = getStream();
  const sessionId = MathUtils.generateUUID();
  return new Promise(function (resolve) {
    const str = { function: 'getSaveDistance', ToSave: `${name}`, sessionId: sessionId };
    stream.emitUIInteraction(str);
    const getGlbPosition = (data) => {
      // {"ModelName":"","UELocation":""}
      const returnData = JSON.parse(data);
      console.log('测量数据保存--', returnData);
      if (returnData.sessionId === sessionId) {
        console.log('模型位置', returnData);
        // stream.removeResponseEventListener(`dataSmithPosition${sessionId}`);
        resolve(returnData);
      }
    };
    stream.addResponseEventListener(`dataSmithPosition${sessionId}`, getGlbPosition);
  });
};
export const getHistory = (data) => {
  const stream = getStream();
  const sessionId = MathUtils.generateUUID();
  return new Promise(function (resolve) {
    const str = { function: 'getHistory', data: `${data}`, sessionId: sessionId };
    stream.emitUIInteraction(str);
    const getGlbPosition = (data) => {
      // {"ModelName":"","UELocation":""}
      const returnData = JSON.parse(data);
      console.log('测量数据保存--', returnData);
      if (returnData.sessionId === sessionId) {
        console.log('模型位置', returnData);
        // stream.removeResponseEventListener(`dataSmithPosition${sessionId}`);
        resolve(returnData);
      }
    };
    stream.addResponseEventListener(`dataSmithPosition${sessionId}`, getGlbPosition);
  });
};
export const getWarningInfo = (event) => {
  const stream = getStream();
  const sessionId = MathUtils.generateUUID();
  return new Promise(function (resolve) {
    const str = { function: 'getWarningInfo', event: event, sessionId: sessionId };
    stream.emitUIInteraction(str);
    const getGlbPosition = (data) => {
      // {"ModelName":"","UELocation":""}
      const returnData = JSON.parse(data);
      console.log('测量数据保存--', returnData);
      if (returnData.sessionId === sessionId) {
        console.log('模型位置', returnData);
        // stream.removeResponseEventListener(`dataSmithPosition${sessionId}`);
        resolve(returnData);
      }
    };
    stream.addResponseEventListener(`dataSmithPosition${sessionId}`, getGlbPosition);
  });
};
export const transmitHistory = (event) => {
  const stream = getStream();
  const sessionId = MathUtils.generateUUID();
  return new Promise(function (resolve) {
    const str = { function: 'transmitHistory', HistoryData: event, sessionId: sessionId };
    stream.emitUIInteraction(str);
    const getGlbPosition = (data) => {
      // {"ModelName":"","UELocation":""}
      const returnData = JSON.parse(data);
      console.log('测量数据保存--', returnData);
      if (returnData.sessionId === sessionId) {
        console.log('模型位置', returnData);
        // stream.removeResponseEventListener(`dataSmithPosition${sessionId}`);
        resolve(returnData);
      }
    };
    stream.addResponseEventListener(`dataSmithPosition${sessionId}`, getGlbPosition);
  });
};
export const deleteDistanceData = (id) => {
  const stream = getStream();
  const sessionId = MathUtils.generateUUID();
  return new Promise(function (resolve) {
    const str = { function: 'DeleteDistanceData', id: id, sessionId: sessionId };
    stream.emitUIInteraction(str);
  });
};
export const cancelWarningInfo = (stats) => {
  const stream = getStream();
  return new Promise(function (resolve) {
    const str = { function: 'cancelWarningInfo', WarningVisibility: stats };
    stream.emitUIInteraction(str);
  });
};
//开启/关闭前端消息监听
export const startListenMessage = (fun) => {
  const stream = getStream();
  const getUIMessage = (data) => {
    const returnData = JSON.parse(data);
    console.log(returnData);
    fun(returnData.WebUIcallbackfunction, returnData);
  };
  stream.addResponseEventListener('getUIMessage', getUIMessage);
  // this.listenmessagefun = getUIMessage
};
//开启/关闭前端消息监听
// export const startListenMessage = () => {
//   const stream = getStream();
//   const getUIMessage = (data) => {
//     const returnData = JSON.parse(data);
//     console.log('监听ue那边传来的消息', returnData);
//     // if(returnData.uuid === this.name+','+this.uuid&&panduanci===returnData.WebUIcallbackfunction){
//     //     //确定是相对应的回话
//     //     fun()
//     // }
//   };
//   stream.addResponseEventListener('getUIMessage', getUIMessage);
//   // this.listenmessagefun = getUIMessage
// };
